//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2006
//
// Change highway dialog.
//

# include "StdAfx.h"
# include "Resource.h"
# include "ChangeHighway.h"
# include "UndoRedo.h"
# include "ListView.h"
# include "ParseHelpers.h"
# include "Globals.h"

static int g_iSortColumn = -1;
static bool g_bAccent = true;

static
int CALLBACK _CompareItems (LPARAM _lParam1, LPARAM _lParam2, LPARAM _lParamSort) {
	const HWND hList = reinterpret_cast<HWND> (_lParamSort);

	char str1 [64], str2 [64];
	ListView_GetItemText (hList, _lParam1, g_iSortColumn, str1, 64); 
	ListView_GetItemText (hList, _lParam2, g_iSortColumn, str2, 64);

	// Compare as strings.
	return g_bAccent ? ::strcmp (str1, str2) : ::strcmp (str2, str1);
}

static
INT_PTR CALLBACK _ChangeHighwayProc (HWND _hWnd, UINT _uMsg, WPARAM _wParam, LPARAM _lParam) {
	switch (_uMsg) {
		case WM_INITDIALOG: {
			highway_t ** const ppHighway = reinterpret_cast<highway_t **> (_lParam);
			::SetWindowLong (_hWnd, GWL_USERDATA, reinterpret_cast<LONG> (ppHighway));

			// Prepare list view control for cities.			
			const HWND hList = ::GetDlgItem (_hWnd, IDC_LIST);

			ListView_SetExtendedListViewStyleEx (hList, LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);

			AddColumn (hList, 0, "Highway");
			AddColumn (hList, 1, "Region");
			AddColumn (hList, 2, "Country");

			CCritSec::CLock lock (g_cs);

			size_t cItem = 0;
			for (highways_t::iterator i = g_map.Highways.begin (); i != g_map.Highways.end (); ++ i) {
				const highway_t & highway = * i;
				if (highway.IsDeleted ())
					continue;

				InsertItem (hList, cItem, reinterpret_cast<LPARAM> (& highway), "%s", highway.strName.c_str ());
				SetSubItemText (hList, cItem, 1, "%s", highway.pRegion->strName.c_str ());
				SetSubItemText (hList, cItem, 2, "%s", highway.pRegion->pCountry->strName.c_str ());

				if (& highway == * ppHighway)
					SetSelection (hList, cItem);

				++ cItem;
			}

			// Set the best width.
			AutoSizeColumns (hList);

			if (g_iSortColumn != -1)
				ListView_SortItemsEx (hList, _CompareItems, hList);

			// Make sure that current highway is visible.
			EnsureSelectionVisible (hList);

			return TRUE;
		}

		case WM_NOTIFY: {
			const NMHDR * const pInfo = reinterpret_cast<const NMHDR *> (_lParam);
			switch (pInfo->code) {
				case LVN_COLUMNCLICK: {
					const NMLISTVIEW * const pInfo2 = reinterpret_cast<const NMLISTVIEW *> (_lParam);

					if (g_iSortColumn != pInfo2->iSubItem)
						g_iSortColumn = pInfo2->iSubItem;
					else
						g_bAccent = ! g_bAccent;
					
					ListView_SortItemsEx (pInfo->hwndFrom, _CompareItems, pInfo->hwndFrom);

					break;
				}

				case NM_CLICK:
				case LVN_KEYDOWN:
					::EnableWindow (::GetDlgItem (_hWnd, IDOK), TRUE);
					::EnableWindow (::GetDlgItem (_hWnd, IDC_APPLY), TRUE);
					break;

				case NM_RETURN:
				case NM_DBLCLK:
					::SendMessage (_hWnd, WM_COMMAND, IDOK, 0);
					break;
			}
			break;
		}

		case WM_COMMAND:
			switch (LOWORD (_wParam)) {
				case IDOK: {
					const HWND hList = ::GetDlgItem (_hWnd, IDC_LIST);
					// Get new highway.
					const size_t cSel = ListView_GetSelectionMark (hList);
					if (cSel == -1)
						break;
					highway_t * const pHighway = reinterpret_cast<highway_t *> (GetLParam (hList, cSel));

					highway_t ** const ppHighway = reinterpret_cast<highway_t **> (::GetWindowLong (_hWnd, GWL_USERDATA));
					* ppHighway = pHighway;

					::EndDialog (_hWnd, 0);
					return TRUE;
				}

				case IDCANCEL:
					// On closing dialog.
					::EndDialog (_hWnd, 1);
					return TRUE;
			}
	}

	return FALSE;
}

//////////////////////////////////////////////////

bool ShowChangeHighway (HWND _hWnd, highway_t * & _pHighway) {
	return DlgBox (IDD_SELECT_HIGHWAY, _hWnd, & _ChangeHighwayProc, reinterpret_cast<LPARAM> (& _pHighway)) == 0;
}
